// CLI Native Test App.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include "EnclaveBridge.h"
#include <stdio.h>

int Exit(int code);

// Our reference vault implemented as memory buffers

int header_size = 86;
const char header[86] = {
	0x65, 0x9b, 0xf2, 0x76, 0x9e, 0x63, 0x8f, 0x84, 0x7b, 0xa1, 0x43, 0x1e, 0x28, 0x4b, 0x3e, 0x30,
	0xa1, 0x30, 0xe7, 0xd7, 0xce, 0x92, 0x00, 0x2e, 0xb6, 0x1b, 0x54, 0x1e, 0x57, 0x26, 0x2c, 0xcf,
	0xa7, 0x39, 0xc8, 0x97, 0x25, 0xe0, 0x12, 0x05, 0x2a, 0xe3, 0x2b, 0x84, 0x13, 0xb3, 0x99, 0x81,
	0xfc, 0x33, 0xca, 0x80, 0x01, 0x00, 0x27, 0x01, 0x00, 0x00, 0xf8, 0x40, 0x2f, 0xb7, 0xe9, 0x4a,
	0xe6, 0x53, 0xe8, 0x8b, 0x94, 0x2e, 0xd5, 0x7e, 0xe1, 0x75, 0xd5, 0x00, 0x48, 0xcd, 0xa5, 0xcc,
	0x08, 0x82, 0x84, 0xbf, 0x8e, 0x49
};
const unsigned char vault_data[299] = {
	0x5e, 0x07, 0x4e, 0xd6, 0x5e, 0x89, 0xf4, 0x7c, 0xa9, 0x16, 0xc8, 0x84, 0xbf, 0x5e, 0xb3, 0x2c,
	0x7b, 0xe4, 0x36, 0xfd, 0xce, 0x84, 0xdc, 0xab, 0x8c, 0x0c, 0x4d, 0x41, 0x74, 0x7b, 0x3f, 0x7f,
	0x00, 0x86, 0x54, 0xb7, 0xc6, 0xf2, 0x81, 0xa6, 0x19, 0xaa, 0xb9, 0x2c, 0xe4, 0xfb, 0xe2, 0x39,
	0xdf, 0xc5, 0x72, 0xd3, 0x98, 0x77, 0x17, 0xcc, 0xa1, 0xb4, 0xc6, 0xc5, 0x32, 0x72, 0x9a, 0x72,
	0x8d, 0x9f, 0xf0, 0x11, 0x6e, 0x9f, 0xba, 0x5d, 0x8c, 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 0xf1, 0x3f, 0xa0, 0x63, 0xf1, 0xff, 0xcf, 0x6f, 0xd5, 0xc3, 0xe2, 0x46, 0x5f, 0x19, 0x07,
	0xef, 0x64, 0xe9, 0x1c, 0xec, 0x6e, 0x8e, 0x2a, 0x78, 0xf5, 0xa5, 0x49, 0x39, 0x8c, 0xd0, 0x67,
	0x23, 0x87, 0xf2, 0x8c, 0x1e, 0xfd, 0x46
};

const char mpass[] = "An enticing aroma of fruit flavors accented by licorice.";
const char new_mpass[] = "Always assured and confident in demeanor.";

int main()
{
	int rv;
	uint32_t count;

	rv = ew_initialize();
	if (rv != 0) {
		fprintf(stderr, "ew_initialize: 0x%08x\n", rv);
		Exit(1);
	}

	rv = ew_initialize_from_header(header, header_size);
	if (rv != 0) {
		fprintf(stderr, "ew_initialize_from_header: 0x%08x\n", rv);
		Exit(1);
	}

	rv = ew_unlock(mpass);
	if (rv != 0) {
		fprintf(stderr, "ew_unlock: 0x%08x\n", rv);
		Exit(1);
	}

	rv = ew_load_vault(vault_data);
	if (rv != 0) {
		fprintf(stderr, "ew_load_vault: 0x%08x\n", rv);
		Exit(1);
	}

	rv = ew_accounts_get_count(&count);
	if (rv != 0) {
		fprintf(stderr, "ew_accounts_get_count: 0x%08x\n", rv);
		Exit(1);
	}
	for (uint32_t i = 0; i < count; ++i) {
		uint16_t sname, slogin, surl, spass;
		char *name, *login, *url, *pass;

		printf("Account %u:\n", i);

		rv = ew_accounts_get_info_sizes(i, &sname, &slogin, &surl);
		if (rv != 0) {
			fprintf(stderr, "ew_accounts_get_info_sizes[%u]: 0x%08x\n", i, rv);
			continue;
		}

		name = new char[sname+1];
		login = new char[slogin+1];
		url = new char[surl+1];

		rv = ew_accounts_get_info(i, name, sname, login, slogin, url, surl);
		if (rv != 0) {
			fprintf(stderr, "ew_accounts_get_info[%u]: 0x%08x\n", i, rv);
			continue;
		}
		name[sname] = 0;
		login[slogin] = 0;
		url[surl] = 0;

		printf("\tname= %s\n", name);
		printf("\tlogin= %s\n", login);
		printf("\turl= %s\n", url);

		rv = ew_accounts_get_password_size(i, &spass);
		if (rv != 0) {
			fprintf(stderr, "ew_accounts_get_password_size[%u]: 0x%08x\n", i, rv);
			continue;
		}

		pass = new char[spass + 1];

		rv = ew_accounts_get_password(i, pass, spass);
		if (rv != 0) {
			fprintf(stderr, "ew_accounts_get_password[%u]: 0x%08x\n", i, rv);
			continue;
		}

		pass[spass] = 0;

		printf("\tpass= %s\n", pass);

		delete[] name;
		delete[] login;
		delete[] url;
		delete[] pass;
	}

	// Start changing things

	// Add an account

	rv = ew_accounts_set_info(3, "Acme", 4, "wileye", 6, "http://acme.nodomain/", 21);
	if (rv != 0) {
		fprintf(stderr, "ew_accounts_set_info[3]: 0x%08x\n", rv);
		Exit(1);
	}

	// Change the passwords many times

	for (int i = 0; i < 20000; ++i) {
		uint32_t idx = i % 4;
		char newpass[32];
		int rv;

		rv = ew_accounts_generate_password(31, 0xffff, newpass);
		if (rv != 0) {
			fprintf(stderr, "ew_accounts_generate_password[%d]: 0x%08x\n", i, rv);
			Exit(1);
		}

		rv = ew_accounts_set_password(idx, newpass, 31);
		if (rv != 0) {
			fprintf(stderr, "ew_accounts_set_password[%d]: 0x%08x\n", i, rv);
			Exit(1);
		}

	}

	// Change the master password

	rv = ew_change_master_password(mpass, new_mpass);
	if (rv != 0) {
		fprintf(stderr, "ew_change_master_password: 0x%08x\n", rv);
		Exit(1);
	}

	Exit(0);
}

int Exit(int code)
{
	printf("Press ENTER to exit...\n");
	getchar();
	exit(code);
}